package tests;

import java.io.IOException;

import va_btree.VABTreeFile;
import global.Batch;
import global.BatchIndex;
import global.CommandLine;
import global.IDXSchema;
import global.IndexType;
import global.Schema;
import global.SystemDefs;

public class MYTest2 {

	   public static void main (String argv[]) {

		   VABTreeFile.quietMode = true;
		   //IDXSchemaTests();
		   batchcreateTests();
		   batchinsertTests();
		   createindexTests();
		   createBindexTest();
		   insertWithIndexTest();
	   }

	   private static void IDXSchemaTests() {
		   CommandLine.initializeDB("MYTEST.db", false, 500);
			
			String fileName = IDXSchema.getIDXFileName("RelName", 2, "B", 4);
			if (fileName.equals("RelName2B4"))
			{
				System.out.println("getIDXFileName test successful");
			}
			else
			{
				System.err.format("getIDXFileName test failed. Expected: 'RelName2B4', 'Actual: %s'\n", fileName);
			}

			IDXSchema index = IDXSchema.GetIndex(fileName);
			if (index==null)
			{
				System.out.println("GetIndex returned null as expected.");
			}
			
			// try to insert an index
			index = new IDXSchema("RelName", 2, "B", 4);
			if (index.InsertIndex())
			{
				System.out.println("Index inserted successfully");
			}
			else
			{
				System.err.println("Index insertion failed.");
			}

			// try to get the index to make sure it is there
			IDXSchema index2 = IDXSchema.GetIndex(fileName);
			if (index2!=null)
			{
				System.out.println("Index was retrieved successfully.");
			}
			else
			{
				System.err.println("GetIndex failed.");
			}
			
			// verify index attributes
			if (index2.RelName.equals("RelName") && index2.ColumnID==2 && index2.IDXType==IndexType.VA_BT_Index
					&& index2.Bits==4 && index2.IDXFileName.equals(fileName))
			{
				System.out.println("Index was retrieved correctly");
			}
			else
			{
				System.err.println("Index values are not correct.");
			}
			
			// add some more indexes so we can test GetIndexes
			index = new IDXSchema("RelName", 4, "B", 4);
			index.InsertIndex();
			index = new IDXSchema("RelName", 2, "H", 4);
			index.InsertIndex();
			index = new IDXSchema("RelName", 2, "B", 8);
			index.InsertIndex();
			index = new IDXSchema("RelName2", 2, "B", 4);
			index.InsertIndex();
			index = new IDXSchema("RelName2", 2, "H", 4);
			index.InsertIndex();

			// get indexes for RelName, should be 4
			IDXSchema[] results = IDXSchema.GetIndexes("RelName");
			if (results.length==4)
			{
				System.out.println("GetIndexes returned all indexes successfully");
			}
			else
			{
				System.err.format("GetIndexes failed. Expected: %d indexes. Actual: %d", 4, results.length);
			}
			
			if (results[2].IDXType != IndexType.VA_Index)
			{
				System.err.println("Wrong index type for heap file va index.");
			}
			
			// get indexes for RelName2, should be 2
			IDXSchema[] results2 = IDXSchema.GetIndexes("RelName2");
			if (results2.length==2)
			{
				System.out.println("GetIndexes returned all indexes successfully");
			}
			else
			{
				System.err.format("GetIndexes failed. Expected: %d indexes. Actual: %d", 2, results2.length);
			}
			
			
			// close and reopen the database to make sure that the information was persisted.
			CommandLine.finalizeCommand();

			CommandLine.initializeDB("MYTEST.db", true, 500);

			// try to get the index to make sure it is there
			IDXSchema index3 = IDXSchema.GetIndex(fileName);
			if (index3!=null)
			{
				System.out.println("Index was retrieved successfully.");
			}
			else
			{
				System.err.println("GetIndex failed.");
			}

			
			CommandLine.finalizeCommand();
	   }

	   private static void batchcreateTests() {
		   CommandLine.dbPath = "MYTEST.db";
		   CommandLine.initializeDB(false);
		   try
		   {
			   if (Batch.Create("/home/mark/sampledata", "MYREL1"))
			   {
				   System.out.println("**** Batchcreate with new DB succeeded.");
			   }
			   else
			   {
				   System.out.println("**** Batchcreate with new DB failed.");
			   }
		   }
		   catch (Exception e)
		   {
			   System.out.println("**** Batchcreate with new DB failed.");
			   e.printStackTrace();
		   }

		   CommandLine.finalizeCommand();
		   CommandLine.initializeDB(true);
		   
		   try
		   {
			   if (Batch.Create("/home/mark/sampledata4", "MYREL2"))
			   {
				   System.out.println("**** Batchcreate with existing DB succeeded.");
			   }
			   else
			   {
				   System.out.println("**** Batchcreate with existing DB failed.");
			   }
		   }
		   catch (Exception e)
		   {
			   System.out.println("Batchcreate with existing DB failed.");
			   e.printStackTrace();
		   }
		   
		   CommandLine.finalizeCommand();
	   }
	   
	   private static void batchinsertTests()
	   {
		   CommandLine.dbPath = "MYTEST.db";
		   CommandLine.initializeDB();
		   
		   try
		   {
			   if (Batch.Insert("/home/mark/sampledata2", "MYREL1", false))
			   {
				   System.out.println("Batchinsert with no indexes succeeded.");
			   }
			   else
			   {
				   System.out.println("Batchinsert with no indexes failed.");
			   }
		   }
		   catch (Exception e)
		   {
			   System.out.println("**** Batchinsert with no indexes failed.");
			   e.printStackTrace();
		   }
		   CommandLine.finalizeCommand();
	   }
	   
	   private static void createBindexTest()
	   {
		   CommandLine.dbPath = "MYTEST.db";
		   CommandLine.initializeDB();
		   try
		   {
			   IDXSchema index = new IDXSchema("MYREL1", 2, "B", 4);
			   if (BatchIndex.CreateIndex("MYREL1", index))
			   {
				   System.out.println("**** Creation of B-tree index succeeded.");
			   }
			   else
			   {
				   System.err.println("**** Creation of B-tree index failed.");
			   }
		   } catch (Exception e) {
			   // TODO Auto-generated catch block
			   System.err.println("**** Creation of B-tree index failed.");
			   e.printStackTrace();
		   }
		   CommandLine.finalizeCommand();
		   
		   // verify indexes in catalog
		   CommandLine.initializeDB();
		   
		   IDXSchema index = IDXSchema.GetIndex("MYREL12B4");
		   if (index!=null)
		   {
			   System.out.println("**** B-tree Index successfully found in catalog.");
		   }
		   else
		   {
			   System.err.println("**** B-tree Index information missing from catalog.");
		   }

		   CommandLine.finalizeCommand();		   
	   }
	   
	   private static void createindexTests()
	   {
		   CommandLine.dbPath = "MYTEST.db";
		   CommandLine.initializeDB();
		   
		   try
		   {
			   IDXSchema index = new IDXSchema("MYREL1", 2, "H", 4);
			   if (BatchIndex.CreateIndex("MYREL1", index))
			   {
				   System.out.println("**** Creation of VA heap file index succeeded.");
			   }
			   else
			   {
				   System.err.println("**** Creation of VA heap file index failed.");
			   }
		   } catch (Exception e) {
			   // TODO Auto-generated catch block
			   System.err.println("**** Creation of VA heap file index failed.");
			   e.printStackTrace();
		   }
		   
		   CommandLine.finalizeCommand();
		   
		   // verify indexes in catalog
		   CommandLine.initializeDB();
		   
		   IDXSchema[] indexes = IDXSchema.GetIndexes("MYREL1");
		   
		   if (indexes.length == 1)
		   {
			   IDXSchema index = IDXSchema.GetIndex("MYREL12H4");
			   if (index!=null)
			   {
				   System.out.println("**** Indexes successfully found in catalog.");
			   }
			   else
			   {
				   System.err.println("**** Index information missing from catalog.");
			   }
		   }

		   CommandLine.finalizeCommand();
	   }
	   
	   private static void insertWithIndexTest()
	   {
		   CommandLine.dbPath = "MYTEST.db";
		   try
		   {
			   IDXSchema index = new IDXSchema("MYREL2", 2, "H", 4);
			   IDXSchema index2 = new IDXSchema("MYREL2", 2, "B", 4);
			   if (createIndexTest("MYREL2", index) && createIndexTest("MYREL2", index2))
			   {
				   CommandLine.initializeDB();
				   if (Batch.Insert("/home/mark/sampledata2", "MYREL2", false))
				   {
					   System.out.println("**** Batchinsert with indexes succeeded.");
				   }
				   else
				   {
					   System.out.println("**** Batchinsert with indexes failed.");
				   }
				   CommandLine.finalizeCommand();
			   }
			   else
			   {
				   System.err.println("**** Couldn't create index to test insert with index.");
			   }
		   } catch (Exception e) {
			   // TODO Auto-generated catch block
			   System.err.println("**** Batchinsert with indexes failed.");
			   e.printStackTrace();
		   }
		   
	   }
	   
	   private static boolean createIndexTest(String relName, IDXSchema index)
	   {
		   boolean result = false;
		   CommandLine.initializeDB();
		   try
		   {
			   result = BatchIndex.CreateIndex(relName, index);
		   }
		   catch (Exception e){}
		   
		   CommandLine.finalizeCommand();
		   return result;
	   }
}
